const ROOT_PATH = process.cwd();
require('best-require')(null, {
  routes: ROOT_PATH + '/app/routes',
  models: ROOT_PATH + '/app/models',
  controllers: ROOT_PATH + '/app/controllers',
  utils: ROOT_PATH + '/app/utils',
  JWT: ROOT_PATH + '/app/utils/JWT',
  config: ROOT_PATH + '/app/config',
});
const JWT = require(':JWT')
const Koa = require('koa')
const router = require(':routes')
const koaBody = require('koa-body')
const koaStatic = require('koa-static')
const mongoose = require('mongoose')
const { dbUrl } = require(':config')
const path = require('path')
const parameter = require('koa-parameter')
console.log(JWT)
// mongoose.connect(dbUrl, { useNewUrlParser: true }, function(err, connection) {
//   connection.db.admin().listDatabases().then(res => {
//     var dbs = res.databases.map(function(base) {
//       return base.name
//         })
//     console.log(dbs)
//     }).catch(err => {
//     console.log(err)
//     })
// })

mongoose.connect(dbUrl, { useNewUrlParser: true }, () => {
  console.log('数据库连接成功')
})
mongoose.connection.on('error', () => {
  console.log('数据库连接失败')
})

// const MongoClient = require('mongodb').MongoClient;
// const uri = "mongodb+srv://xv700:<password>@cluster0-2eqjw.azure.mongodb.net/test?retryWrites=true&w=majority";
// const client = new MongoClient(uri, { useNewUrlParser: true });
// client.connect(err => {
//   const collection = client.db("test").collection("devices");
//   // perform actions on the collection object
//   client.close();
// });
const app = new Koa()

app.use(koaStatic(path.join(__dirname, 'public')))

app.use(parameter(app))
// app.use(bodyparser()) // 解析post请求
app.use(koaBody({
  multipart: true, // 启用文件，支持文件的格式
  formidable: {
    uploadDir: path.join(__dirname, '/public/uploads'),
    // 保留拓展名, 默认是 false
    keepExtensions: true
  }
}))
// app.use(koaBody({
//     multipart: true,
//     formidable: {
//         maxFileSize: 200*1024*1024    // 设置上传文件大小最大限制，默认2M
//     }
// }))

router(app)

app.listen(3000, () => {
  console.log('your server is listening at port 3000')
})
